from .models import *
from rest_framework import serializers

class BrandSer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    logo = serializers.CharField(allow_null=True)
    class Meta:
        model = Brand
        fields = "__all__"

class GroupSer(serializers.ModelSerializer):
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.all())
    cate_name = serializers.SerializerMethodField(read_only=True)

    def get_cate_name(self,obj):
        return obj.cate.name

    class Meta:
        model = Group
        fields = '__all__'
        read_only_fields = ["id",]

class CateSer(serializers.ModelSerializer):
    class Meta:
        model = Cate
        fields = "__all__"

class SKUSer(serializers.ModelSerializer):
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.all())
    cate_name = serializers.SerializerMethodField(read_only=True)
    def get_cate_name(self,obj):
        return obj.cate.name


    class Meta:
        model = SKU
        fields = '__all__'
        read_only_fields = ['id','detail','image_default','sales']

class SPUSer(serializers.ModelSerializer):
    brand = serializers.PrimaryKeyRelatedField(queryset=Brand.objects.all())
    brand_name = serializers.SerializerMethodField(read_only=True)

    def get_brand_name(self,obj):
        return obj.brand.name
    class Meta:
        model = SPU
        fields = '__all__'
        read_only_fields = ['id',]

class SPUSpecificationSer(serializers.ModelSerializer):
    spu = serializers.PrimaryKeyRelatedField(queryset=SPU.objects.all())
    spu_name = serializers.SerializerMethodField(read_only=True)
    def get_spu_name(self,obj):
        return obj.spu.name
    class Meta:
        model = SPUSpecification
        fields = "__all__"
        read_only_fields = ['id',]

class SpecificationOptionSer(serializers.ModelSerializer):
    spec = serializers.PrimaryKeyRelatedField(queryset=SPUSpecification.objects.all())
    spec_name = serializers.SerializerMethodField(read_only=True)
    def get_spec_name(self,obj):
        return obj.spec.name

    class Meta:
        model = SpecificationOption
        fields = '__all__'
        read_only_fields = ['id',]

class SKUImageSer(serializers.ModelSerializer):
    sku = serializers.PrimaryKeyRelatedField(queryset=SKU.objects.all())
    sku_name = serializers.SerializerMethodField(read_only=True)
    def get_sku_name(self,obj):
        return obj.sku.name
    class Meta:
        model = SKUImage
        fields = '__all__'
        read_only_fields = ['id',]